package com.kindness.frate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;
import android.widget.ImageView;

import com.kindness.frate.R;
import com.kindness.frate.base.KindnessApplication;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.base.BaseDialog;
import com.kindnesslibrary.dialog.ChooseImageDialog;
import com.kindnesslibrary.util.Contants;
import com.kindnesslibrary.util.ScreenUtil;
import com.kindnesslibrary.ximage.XImgSelConfig;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 头像选择
 */
public class AvatarChooseActivity extends BaseActivity {

    private ImageView iv_avatar_choose_image;
    ChooseImageDialog dialog;

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        iv_avatar_choose_image = (ImageView) findViewById(R.id.iv_avatar_choose_image);
    }

    @Override
    protected void initEvent() {
        iv_avatar_choose_image.setOnClickListener(v -> showDialog());
    }

    /**
     * 显示选择照片dialog
     */
    private void showDialog() {
        dialog = new ChooseImageDialog(this, R.style.dialogTheme, ScreenUtil.getScreenWidth(this), BaseDialog.UN_CHANGED);
        dialog.setCancelable(true);
        dialog.getWindow().setWindowAnimations(R.style.ActionSheetDialogAnimation);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.setxImgSelConfig(new XImgSelConfig.Builder(null).maxNum(1).showCamera(false).isCrop(true).isPreview().build());
        dialog.setOnImageDialogListener(new ChooseImageDialog.OnImageDialogListener() {
            @Override
            public void camera(String path) {
                ImageLoader.getInstance().displayImage("file://" + path, iv_avatar_choose_image, KindnessApplication.getInstance().options);
            }

            @Override
            public void album(List<String> albumList) {
                ImageLoader.getInstance().displayImage("file://" + albumList.get(0), iv_avatar_choose_image, KindnessApplication.getInstance().options);
            }
        });
        dialog.show();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_avatar_choose_layout;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode == Contants.CAMERA_CODE_RESULT){
                dialog.onChooseImageListener.cameraResult();
            } else if(requestCode == Contants.CAMERA_CODE_ZOOM){
                dialog.onChooseImageListener.cameraZoom();
            } else if(requestCode == Contants.ALBUM_CONTENT_DATA_CODE){
                dialog.onChooseImageListener.album(data);
            }
        }
    }

    @Override
    protected void onCameraPermissionsResultOK() {
        super.onCameraPermissionsResultOK();
        dialog.onChooseImageListener.camera();
    }

}
